<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="javax.servlet.http.*"%>
<%
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ request.getContextPath() + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<title>Insert title here</title>
</head>
<body>
	<h1>登陆成功！</h1>
	<%request.setCharacterEncoding("utf-8");
	//首先判断用户是否选择了记住登录状态
	String[] isusecookies = request.getParameterValues("isusecookie");
	if(isusecookies!=null&&isusecookies.length>0) {//说明用户选了记住登录状态的按钮，这时候我们就要保存cookie了，就要生成两个cookie对象
		//把用户名和密码分别保存在两个Cookie对象里
		//获得用户名：编码，解决无法在Cookie中保存中文字符串的问题
		String name = URLEncoder.encode(request.getParameter("username"),"utf-8");
		String pw = URLEncoder.encode(request.getParameter("password"),"utf-8");
		Cookie usernamecookie = new Cookie("username",name);
		Cookie passwordcookie = new Cookie("password",pw);
		usernamecookie.setMaxAge(3600);//设置最大生存期限为1h
		passwordcookie.setMaxAge(3600);
		//分别保存这两个Cookie对象
		response.addCookie(usernamecookie);
		response.addCookie(passwordcookie);
				
	}
	else {//把已经保存的Cookie对象设置为失效
		Cookie[] cookies = request.getCookies();
		if(cookies!=null&&cookies.length>0) {//说明曾经保存过cookie
			for(Cookie c : cookies) { //判断是不是用户名或密码的Cookie对象
				if(c.getName().equals("username")||c.getName().equals("password")) {
				c.setMaxAge(0);//生存期限设置为0，即为失效
				response.addCookie(c);//还要重新保存cookie，否则失效不会起作用
				}
			}
		}
		
	}


%>
	<a href="testcookie/user.jsp">查看用户信息</a>
</body>
</html>